package cn.edu.ujn.lab3.c;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.edu.ujn.lab3.dao.User;
import cn.edu.ujn.lab3.service.IUserService;

@Controller
public class UserController {
	@Autowired
	private IUserService userService; 
	
	/**
	 * 用户登录
	 */
	@RequestMapping(value = "/login.action", method = RequestMethod.POST)
	public String login(String usercode,String password,Model model,HttpSession session) {
		User findUser = this.userService.findUser(usercode, password);
		if(findUser!=null) {
			session.setAttribute("USER_SESSION", findUser);
			return "customer";
			//return "redirect:customer/list.action";
		}
		model.addAttribute("msg","<script>alert('用户名或密码错误，请重新登陆');</script>");
		return "login";
	}
	
	/**
	 * 模拟其他类中跳转到客户管理页面的方法
	 */
	@RequestMapping(value = "/toCustomer.action")
	public String toCustomer() {
	    return "customer";
	}
	
	/**
	 * 退出登录
	 */
	@RequestMapping(value = "/logout.action")
	public String logout(HttpSession session) {
	    // 清除Session
	    session.invalidate();
	    // 重定向到登录页面的跳转方法
	    return "redirect:login.action";
	}

	/**
	 * 向用户登陆页面跳转
	 */
	@RequestMapping(value = "/login.action", method = RequestMethod.GET)
	public String toLogin() {
	    return "login";
	}
}
